from django.views import View

from User.models import Order
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredVenue


class VerfiyYardView(LoginRequiredVenue, View):
    """ 校验订单场地 """

    @Req.getJson
    def post(self, request):
        """ 校验 """
        orderId = request.json_dict.get("orderId")
        verify = request.json_dict.get("verify")

        venue = request.user.venue

        order = Order.objects.filter(oid=orderId).first()
        if not order:
            return Res.Json(Res.ParamErr)

        orderYard = order.orderyard_set.first()
        if orderYard.getTime:
            return Res.Json(Res.Warn, msg="已校验")
        if not orderYard.verifyCode(verify):
            return Res.Json(Res.Warn, msg="校验码错误")

        return Res.Json(Res.OK)
